package com.yyge.exception;

import com.yyge.dto.R;
import com.yyge.dto.ResultCode;
import lombok.extern.log4j.Log4j2;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.servlet.NoHandlerFoundException;

@Log4j2
@RestControllerAdvice
public class ApplicationExceptionHandler {
   /**
   * 自定义异常
   * @param e
   */
   @ExceptionHandler(ApplicationException .class )
   public R handleApplicationException (ApplicationException e) {
     log .error (e);
     return R.error(e.getResultCode ());
  }
   /**
   * 404
   * @return
   */
   @ExceptionHandler (NoHandlerFoundException.class )
   public R handleNotFound (NoHandlerFoundException e) {
     log.error (e);
     return R.error(ResultCode.NOT_FOUND,e.getMessage());
  }
   /**
   * 其他异常 (兜底 )
   * @param e
   * @return
   */
   @ExceptionHandler (Exception .class )
   public R handleException (Exception e) {
     log .error (e);
     return R.error(ResultCode.ERROR,e.getMessage());
  }

}
